﻿package 
{
	import away3d.cameras.Camera3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.View3D;
	import away3d.core.render.Renderer;
	import away3d.core.utils.Cast;
	import away3d.loaders.Collada;
	import away3d.loaders.Loader3D;
	import away3d.loaders.data.AnimationData;
	import away3d.materials.BitmapFileMaterial;
	import away3d.webcamFaceDetector.src.WebcamFaceDetector;

	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import away3d.materials.GlassMaterial;

	public class demo3 extends Sprite
	{
		//the material array
		//private var material:Array=[["model/clown.DAE","model/clown.JPEG"],["model/crown.dae","model/crown.jpg"]];
		private var material:Array=new Array("dae/clown.DAE","dae/crown.DAE","dae/eyeshade2 - 副本.DAE",
		 "dae/eyeshade.DAE","dae/gas mask.DAE","dae/Glass.DAE","dae/hat.DAE",
		 "dae/long hat.DAE","dae/mustache.DAE","dae/pig nose.DAE");
		//private var select:int = 0;
		private var modelArray:Array;
		private var count:int;

		private var _view:View3D;
		private var _camController:HoverDragController;
		private var _dae:ObjectContainer3D;
		//private var daeArray:Array;
		private var _container:ObjectContainer3D;

		//webcamface detector
		private var face_detect:WebcamFaceDetector;
		private var detectTimer:Timer;
		public var clown_ins:clown;
		public var crown_ins:crown;
		public var eyeshade_ins:eyeshade;
		public var eyeshade2_ins:eyeshade2;
		public var gasMask_ins:gasMask;
		public var glass_ins:glass;
		public var hat_ins:hat;
		public var magicHat_ins:magicHat;
		public var mustache_ins:mustache;
		public var nose_ins:nose;
		public function demo3()
		{
			//select = 0;
			for (var i:int=0;i<10;++i)
			{
				trace(material[i]);
			}
			init();
		}
		private function init():void
		{
			//LoadXML();
			InitButton();
			_view = new View3D({x:300,y:300});
			//_camController = new HoverDragController(_view.camera, stage);
			//_camController.radius = 500;

			LoadDae();

			_view.scene.addChild(_container);

			this.addChild(_view);

			//face detect
			face_detect = new WebcamFaceDetector  ;
			if ((face_detect == null))
			{
				trace("face null!!!");
			}
			this.addChildAt(face_detect,0);

			//update
			detectTimer = new Timer(50,0);
			detectTimer.addEventListener(TimerEvent.TIMER,detect_timer);
			detectTimer.start();

		}
		private function detect_timer(e:TimerEvent):void
		{
			//face detect
			if ((face_detect == null))
			{
				trace("no face detect!");
			}
			if (face_detect._rects != null && face_detect._rects.length > 0)
			{
				var faceRect = face_detect._rects[0] as Sprite;
				//_container.x=faceRect.x-faceRect.width;
				//_container.y=-faceRect.y+faceRect.height;
				_container.x = faceRect.x;
				_container.y =  -  faceRect.y;
				//trace(face_detect._rects.length);
				//trace("x: "+faceRect.x+" "+colladaContainer.x);
				//trace("y: "+faceRect.y+" "+colladaContainer.y);
				//trace("changed length: "+face_detect._rects.length);
			}

			//_container.rotationY += 1;
			//_container.rotationX += .1;
			_view.render();
		}
		private function LoadDae():void
		{
			_container = new ObjectContainer3D  ;
			//daeArray=new Array();
			for (var i:int=0; i<10; i++)
			{
				_dae = Collada.load(material[i],{mouseEnabled:false,scaling:"60"});
				_dae.x = -200;
				_dae.y = 85;
				//daeArray[i]=_dae;
				_container.addChild(_dae);
				_container.children[i].visible = false;
				//_container.addChild(daeArray[i]);
			}
		}
		private function InitButton():void
		{
			clown_ins=new clown();
			clown_ins.x = 511.5;
			clown_ins.y = 24;
			crown_ins=new crown();
			crown_ins.x = 511.5;
			crown_ins.y = 58;
			eyeshade_ins=new eyeshade();
			eyeshade_ins.x = 511.5;
			eyeshade_ins.y = 92;
			eyeshade2_ins=new eyeshade2();
			eyeshade2_ins.x = 511.5;
			eyeshade2_ins.y = 126;
			gasMask_ins=new gasMask();
			gasMask_ins.x = 511.5;
			gasMask_ins.y = 160;
			glass_ins=new glass();
			glass_ins.x = 511.5;
			glass_ins.y = 194;
			hat_ins=new hat();
			hat_ins.x = 511.5;
			hat_ins.y = 228;
			magicHat_ins=new magicHat();
			magicHat_ins.x = 511.5;
			magicHat_ins.y = 262;
			mustache_ins=new mustache();
			mustache_ins.x = 511.5;
			mustache_ins.y = 296;
			nose_ins=new nose();
			nose_ins.x = 511.5;
			nose_ins.y = 330;
			addChild(clown_ins);
			addChild(crown_ins);
			addChild(eyeshade_ins);
			addChild(eyeshade2_ins);
			addChild(gasMask_ins);
			addChild(glass_ins);
			addChild(hat_ins);
			addChild(magicHat_ins);
			addChild(mustache_ins);
			addChild(nose_ins);
			
			clown_ins.addEventListener(MouseEvent.CLICK,on_clown);
			crown_ins.addEventListener(MouseEvent.CLICK,on_crown);
			eyeshade_ins.addEventListener(MouseEvent.CLICK,on_eyeshade);
			eyeshade2_ins.addEventListener(MouseEvent.CLICK,on_eyeshade2);
			gasMask_ins.addEventListener(MouseEvent.CLICK,on_gasMask);
			glass_ins.addEventListener(MouseEvent.CLICK,on_glass);
			hat_ins.addEventListener(MouseEvent.CLICK,on_hat);
			magicHat_ins.addEventListener(MouseEvent.CLICK,on_magicHat);
			mustache_ins.addEventListener(MouseEvent.CLICK,on_mustache);
			nose_ins.addEventListener(MouseEvent.CLICK,on_nose);
			
		}
		private function on_clown(e:MouseEvent)
		{
			controlVisible(0);
		}
		private function on_crown(e:MouseEvent)
		{
			controlVisible(1);
		}
		private function on_eyeshade(e:MouseEvent)
		{
			controlVisible(2);
		}
		private function on_eyeshade2(e:MouseEvent)
		{
			controlVisible(3);
		}
		private function on_gasMask(e:MouseEvent)
		{
			controlVisible(4);
		}
		private function on_glass(e:MouseEvent)
		{
			controlVisible(5);
		}
		private function on_hat(e:MouseEvent)
		{
			controlVisible(6);
		}
		private function on_magicHat(e:MouseEvent)
		{
			controlVisible(7);
		}
		private function on_mustache(e:MouseEvent)
		{
			controlVisible(8);
		}
		private function on_nose(e:MouseEvent)
		{
			controlVisible(9);
		}
		private function controlVisible(i:int):void
		{
			if (_container.children[i].visible == true)
			{
				_container.children[i].visible = false;
			}
			else if (_container.children[i].visible==false)
			{
				_container.children[i].visible = true;
			}
		}

	}
}